package cm.server.controller;

import cm.server.dao.PeopleResidenceRepository;
import cm.server.entity.PeopleResidence;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author zy
 * @description
 * @date 2021/4/14 15:03
 */
@RestController
@RequestMapping("areaTrack")
@CrossOrigin
public class PeopleResidenceController {
    @Autowired
    private PeopleResidenceRepository peopleResidenceRepository;

    @RequestMapping(value = "track", method = RequestMethod.GET)
    /**
     *@describtion 用户的区域轨迹
     *@author zy
     *@params [user_id, day]
     *@return java.util.List<cm.server.entity.PeopleResidence>
     *@createTime 2021/4/14 15:07
     */
    public List<PeopleResidence> areaTrack(int user_id, int day, int floor) {
        List<PeopleResidence> pList = peopleResidenceRepository.findTrackByUserId(user_id, day);
        pList = pList.stream().filter(num -> {
            if (floor == 1) {
                return num.getAreaId() <= 17;
            } else {
                return num.getAreaId() > 17;
            }
        }).collect(Collectors.toList());
        int length = pList.size();
        System.out.println();
        List<PeopleResidence> fList = new ArrayList<>();
        for (int i = 0; i < length; ++i) {
            fList.add(pList.get(i));
            if (i < length - 1) {
                int areaId = pList.get(i).getAreaId();
                int nextId = pList.get(i+1).getAreaId();
                int residence = pList.get(i).getResidence();
                while(nextId == areaId) {
                    i++;
                    nextId = pList.get(i+1).getAreaId();
                    residence += pList.get(i+1).getResidence();
                }
                fList.get(fList.size()-1).setResidence(residence);
                fList.get(fList.size()-1).setLeaveTime(pList.get(i).getLeaveTime());
            }
        }
        return fList;
    }
}
